export const outTime = Date.now() + (1000 * 60) * 60;

export const getItem = (item) => {
    let data = window.localStorage.getItem(item);
    if(!data) return data;
    data = data.split('|');
    if(data[1] <= Date.now()){
        return '';
    }
    return data[0];
}

export const setItem = (name,item,date) => {
    return window.localStorage.setItem(name , `${item}|${date}`);
}

export const removeItem = (name) => {
    return window.localStorage.removeItem(name);
}



export const outLogin = () => {
    removeItem('islogin');
    removeItem('username');
}


// 设置cookie，若传递时间则以小时为单位
export const setCookie = (name, value, time, path, domain) => {
    var date = new Date();
    var cookie = '';
    if(name && value){
        cookie = name + "=" +encodeURIComponent(value) +";";
        if(time){
            date.setTime(date.getTime() + Number(time) * 3600 * 1000);
            cookie += "expires=" + date.toGMTString() + ";"; 
        }
        if(domain){
            cookie += "domain=" + domain + ";"; 
        } 
        if(path != null) {
            cookie += "path=" + path + ";"; 
        }
        document.cookie = cookie; 
    }
};

export const getCookie = (key) => {
    var cookie = document.cookie;
    var cookieArray = cookie.split(';');
    var myValue = '';
    for(let i=0; i<cookieArray.length; i++){
       if(cookieArray[i].trim().substr(0,key.length) == key){
            myValue = decodeURIComponent(cookieArray[i].trim().substr(key.length+1));
            break;
       }
    }
    return myValue;
};

